package com.centaline.androidsalesblog.activities.sale;

import com.baidu.location.BDLocation;
import com.baidu.mapapi.model.LatLng;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.base.BaseSaleListActivity;
import com.centaline.androidsalesblog.constants.CommonConstant;
import com.centaline.androidsalesblog.constants.ExtraConstant;
import com.centaline.androidsalesblog.model.businissobject.DropBo;
import com.centaline.androidsalesblog.rx.database.DropSaleRegionFunc1;
import com.centaline.androidsalesblog.rx.database.MetroFunc1;
import com.centaline.androidsalesblog.rx.database.RxDataBase;
import com.centaline.androidsalesblog.rx.database.SearchDataFunc1;
import com.centaline.androidsalesblog.widgets.dropdown.MoreDrop;
import com.centaline.androidsalesblog.widgets.dropdown.SalePriceDrop;
import com.centaline.androidsalesblog.widgets.dropdown.SingleDrop;
import com.centaline.androidsalesblog.widgets.dropdown.TrebleDrop;

import java.util.ArrayList;
import java.util.Locale;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func3;
import rx.functions.Func7;
import rx.schedulers.Schedulers;

/**
 * 周边同价位二手房
 * Created by guilin on 16/1/26.
 */
public class EsfSaleOfNearbyActivity extends BaseSaleListActivity {

    private TrebleDrop trebleDrop;
    private SalePriceDrop salePriceDrop;
    private SingleDrop postRoomDrop;
    private MoreDrop moreDrop;

    @Override
    protected void findViews() {
        setUniversalToolbar(R.string.title_esf_sale_of_nearby);
        super.findViews();
    }

    @Override
    protected void initViews() {
        super.initViews();
        LatLng latLng = getIntent().getParcelableExtra(ExtraConstant.LatLng);
        if (latLng.latitude != 0 &&
                latLng.longitude != 0) {
            postListApi.setLatLng(latLng);
            postListApi.setRound("1000");
            setTab(0, "1km");
        }
        double samePrice = getIntent().getDoubleExtra(ExtraConstant.Price, 0);
        if (samePrice > 0) {
            postListApi.setMin(String.format(Locale.CHINA, "%.0f", samePrice * 0.9));
            postListApi.setMax(String.format(Locale.CHINA, "%.0f", samePrice * 1.1));
            salePriceDrop.setMaxAndMin(String.format(Locale.CHINA, "%.0f", samePrice * 1.1 / 10000),
                    String.format(Locale.CHINA, "%.0f", samePrice * 0.9 / 10000));
            setTab(1, String.format(Locale.CHINA, "%s-%s",
                    String.format(Locale.CHINA, "%.0f", samePrice * 0.9 / 10000),
                    String.format(Locale.CHINA, "%.0f万", samePrice * 1.1 / 10000)));
        }
    }

    @Override
    protected void initDrop() {
        dropDownView.initTabs(R.array.sale);
        trebleDrop = new TrebleDrop(dropDownView, this);
        salePriceDrop = new SalePriceDrop(dropDownView, this);
        postRoomDrop = new SingleDrop(dropDownView, this);
        moreDrop = new MoreDrop(dropDownView, this);
        dropDownView.addDrops(trebleDrop, salePriceDrop, postRoomDrop, moreDrop);

        Observable.combineLatest(RxDataBase.getSaleRegions()
                        .subscribeOn(Schedulers.io())
                        .map(new DropSaleRegionFunc1()),
                RxDataBase.nearby(),
                RxDataBase.getMetro()
                        .subscribeOn(Schedulers.io())
                        .map(new MetroFunc1()),
                new Func3<ArrayList<DropBo>, DropBo, ArrayList<DropBo>, ArrayList<DropBo>>() {
                    @Override
                    public ArrayList<DropBo> call(ArrayList<DropBo> dropBos, DropBo nearBy, ArrayList<DropBo> dropBos3) {
                        final ArrayList<DropBo> list = new ArrayList<>();
                        DropBo dropBo = new DropBo(10);
                        dropBo.setText("区域");
                        dropBo.setValue(CommonConstant.DROP_VALUE_NULL);
                        dropBo.setChildrenList(new ArrayList<DropBo>());
                        DropBo dropBo1 = new DropBo(10);
                        dropBo1.setText(CommonConstant.DROP_TEXT_NO_LIMITED);
                        dropBo1.setValue(CommonConstant.DROP_VALUE_NULL);
                        dropBo.getChildrenList().add(dropBo1);
                        dropBo.getChildrenList().add(nearBy);
                        dropBo.getChildrenList().addAll(dropBos);
                        list.add(dropBo);
                        dropBo = new DropBo(20);
                        dropBo.setText("地铁");
                        dropBo.setValue(CommonConstant.DROP_VALUE_NULL);
                        dropBo.setChildrenList(dropBos3);
                        list.add(dropBo);
                        return list;
                    }
                }).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<ArrayList<DropBo>>() {
                    @Override
                    public void call(ArrayList<DropBo> dropBos) {
                        trebleDrop.init(dropBos);
                    }
                });
        RxDataBase.getSearchData("Sell")
                .map(new SearchDataFunc1(30))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<ArrayList<DropBo>>() {
                    @Override
                    public void call(ArrayList<DropBo> dropBos) {
                        salePriceDrop.init(dropBos);
                    }
                });
        RxDataBase.getSearchData("Room")
                .map(new SearchDataFunc1(40))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<ArrayList<DropBo>>() {
                    @Override
                    public void call(ArrayList<DropBo> dropBos) {
                        postRoomDrop.init(dropBos);
                    }
                });
        Observable.combineLatest(
                RxDataBase.getSearchData("SellOrderby").map(new SearchDataFunc1(100)),
                RxDataBase.getSearchData("Direction").map(new SearchDataFunc1(50)),
                RxDataBase.getSearchData("Area").map(new SearchDataFunc1(60)),
                RxDataBase.getSearchData("Fitment").map(new SearchDataFunc1(70)),
                RxDataBase.getSearchData("SellTag").map(new SearchDataFunc1(80)),
                RxDataBase.getSearchData("Construction").map(new SearchDataFunc1(90)),
                RxDataBase.getSearchData("Property").map(new SearchDataFunc1(110)),
                new Func7<ArrayList<DropBo>, ArrayList<DropBo>, ArrayList<DropBo>, ArrayList<DropBo>,
                        ArrayList<DropBo>, ArrayList<DropBo>, ArrayList<DropBo>, ArrayList<DropBo>>() {
                    @Override
                    public ArrayList<DropBo> call(ArrayList<DropBo> dropBos, ArrayList<DropBo> dropBos2,
                                                  ArrayList<DropBo> dropBos3, ArrayList<DropBo> dropBos4,
                                                  ArrayList<DropBo> dropBos5, ArrayList<DropBo> dropBos6,
                                                  ArrayList<DropBo> dropBos7) {
                        final ArrayList<DropBo> list = new ArrayList<>();
                        DropBo dropBo;
                        if (dropBos.size() > 0) {
                            dropBo = new DropBo(100);
                            dropBo.setText("排序");
                            dropBo.setValue(CommonConstant.DROP_VALUE_NULL);
                            dropBo.setChildrenList(dropBos);
                            list.add(dropBo);
                        }
                        if (dropBos2.size() > 0) {
                            dropBo = new DropBo(50);
                            dropBo.setText("朝向");
                            dropBo.setValue(CommonConstant.DROP_VALUE_NULL);
                            dropBo.setChildrenList(dropBos2);
                            list.add(dropBo);
                        }

                        if (dropBos3.size() > 0) {
                            dropBo = new DropBo(60);
                            dropBo.setText("面积");
                            dropBo.setValue(CommonConstant.DROP_VALUE_NULL);
                            dropBo.setChildrenList(dropBos3);
                            list.add(dropBo);
                        }
                        if (dropBos4.size() > 0) {
                            dropBo = new DropBo(70);
                            dropBo.setText("装修");
                            dropBo.setValue(CommonConstant.DROP_VALUE_NULL);
                            dropBo.setChildrenList(dropBos4);
                            list.add(dropBo);
                        }
                        if (dropBos5.size() > 0) {
                            dropBo = new DropBo(80);
                            dropBo.setText("特色");
                            dropBo.setSelected(true);
                            dropBo.setValue(CommonConstant.DROP_VALUE_NULL);
                            dropBo.setChildrenList(dropBos5);
                            list.add(dropBo);
                        }
                        if (dropBos6.size() > 0) {
                            dropBo = new DropBo(90);
                            dropBo.setText("房龄");
                            dropBo.setValue(CommonConstant.DROP_VALUE_NULL);
                            dropBo.setChildrenList(dropBos6);
                            list.add(dropBo);
                        }
                        if (dropBos7.size() > 0) {
                            dropBo = new DropBo(110);
                            dropBo.setText("类型");
                            dropBo.setValue(CommonConstant.DROP_VALUE_NULL);
                            dropBo.setChildrenList(dropBos7);
                            list.add(dropBo);
                        }
                        return list;
                    }
                }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<ArrayList<DropBo>>() {
                    @Override
                    public void call(ArrayList<DropBo> dropBos) {
                        moreDrop.init(dropBos);
                    }
                });

    }

    @Override
    protected void locationInfo(BDLocation bdLocation) {
        switch (bdLocation.getLocType()) {
            case 61:
            case 161:
                LatLng latLng = new LatLng(bdLocation.getLatitude(), bdLocation.getLongitude());
                saleListFragment.setLocation(latLng);
                postListApi.setLatLng(latLng);
                request();
                break;
            default:
                saleListFragment.setLocation(null);
                saleListFragment.stopRefresh(null);
                break;
        }
    }
}
